﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;

using RexToy;
using RexToy.L10N;

namespace MGAS.Model
{
    using MGAS.Model.Entity;

    public static class AllocMapWriter
    {
        public static void ReWrite(IEnumerable<ISensorData> meta)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml("<?xml version=\"1.0\"?><sensor-map></sensor-map>");

            var xList = doc.CreateElement("id");
            doc.DocumentElement.AppendChild(xList);
            var xBuildings = doc.CreateElement("buildings");
            doc.DocumentElement.AppendChild(xBuildings);
            var xLocations = doc.CreateElement("locations");
            doc.DocumentElement.AppendChild(xLocations);
            var xSensors = doc.CreateElement("sensors");
            doc.DocumentElement.AppendChild(xSensors);

            var ids = (from m in meta
                       where m.Meta
                       select m.ID).ToArray();
            xList.SetAttribute("list", ids.Join(','));

            var buildings = (from m in meta
                             where m.Meta
                             select m.Building).Distinct();
            foreach (var b in buildings)
            {
                var x = doc.CreateElement("building");
                x.SetAttribute("Id", b.ToString());
                x.SetAttribute("Text", string.Format("{0}幢", b));//TODO:L10N
                xBuildings.AppendChild(x);
            }

            List<Location> locs = new List<Location>();
            int i = 0;
            foreach (var b in buildings)
            {
                var floors = (from m in meta
                              where m.Meta && m.Building == b
                              select m.Floor).Distinct();
                
                foreach (var f in floors)
                {
                    i++;
                    var x = doc.CreateElement("location");
                    x.SetAttribute("Id", i.ToString());
                    x.SetAttribute("BuildingId", b.ToString());
                    x.SetAttribute("Floor", f.ToString());
                    x.SetAttribute("Text", string.Format("{0}楼", f));//TODO:L10N
                    xLocations.AppendChild(x);

                    Location l = new Location() { Id = i, Floor = f, BuildingId = b };
                    locs.Add(l);
                }
            }

            foreach (var m in meta)
            {
                if (!m.Meta)
                    continue;

                var x = doc.CreateElement("sensor");
                x.SetAttribute("Id", m.ID.ToString());

                var q = from l in locs
                        where m.Meta && l.BuildingId == m.Building && l.Floor == m.Floor
                        select l.Id;
                x.SetAttribute("LocationId", q.Single().ToString());
                x.SetAttribute("Line", m.LineNumber.ToString());
                //TODO:L10N
                x.SetAttribute("Text", EnumEx.GetDescription(m.Type) + ((m.LineNumber == 1) ? string.Empty : m.LineNumber.ToString()));

                xSensors.AppendChild(x);
            }

            doc.Save(Configuration.GetAllocMap());
        }
    }
}
